#include <UnitTest++.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include "Logger.h"
#include "Util.h"

using namespace std;

void readFile(char* buffer, int size)
{
    FILE * logFile;
    logFile = fopen("Logs.txt", "r");
    if (logFile != NULL)
    {
        if (fgets(buffer, size, logFile) != NULL)
            puts(buffer);
        fclose(logFile);
    }
}

void getLoggerType(char* buffer)
{
    freopen("Logs.txt", "r", stdin);
    scanf("%s", buffer);
}

TEST(TestLogFile)
{
    Logger* logger = new Logger();
    logger->log(DEBUG, __PRETTY_FUNCTION__, "%s", "Loading list");
    delete logger;
    char fileContent[150];
    readFile(fileContent, 150);
    char date[50];
    Util::getDate("%b-%d-%y %H:%M:%S", date, 20);
    strcat(date, "\n");
    char format[80] = "DEBUG: Loading list Function[%s] Thread[%d] %s";
    char expected[150];
    Util::format(expected, format, __PRETTY_FUNCTION__, pthread_self(), date);
    CHECK_EQUAL(expected, fileContent);
    unlink("Logs.txt");
}

TEST(testDebugLevelWithDebugType)
{
    Logger* logger = new Logger();
    logger->log(DEBUG, "%s", "Loading list");
    delete logger;
    char logType[20];
    getLoggerType(logType);
    CHECK_EQUAL("DEBUG:", logType);
    unlink("Logs.txt");
}

TEST(testDebugLevelWithWarningType)
{
    Logger* logger = new Logger();
    logger->log(WARNING, "%s", "Loading list");
    delete logger;
    char logType[20];
    getLoggerType(logType);
    CHECK_EQUAL("WARN:", logType);
    unlink("Logs.txt");
}

TEST(testDebugLevelWithInfoType)
{
    Logger* logger = new Logger();
    logger->log(INFO, "%s", "Loading list");
    delete logger;
    char logType[20];
    getLoggerType(logType);
    CHECK_EQUAL("INFO:", logType);
    unlink("Logs.txt");
}

TEST(testInfoLevelWithDebugType)
{
    Logger* logger = new Logger(INFO);
    logger->log(DEBUG, "%s", "Loading list");
    delete logger;
    char logType[20];
    getLoggerType(logType);
    CHECK(logType);
    unlink("Logs.txt");
}

TEST(testInfoLevelWithInfoType)
{
    Logger* logger = new Logger(INFO);
    logger->log(INFO, "%s", "Loading list");
    delete logger;
    char logType[20];
    getLoggerType(logType);
    CHECK_EQUAL("INFO:", logType);
    unlink("Logs.txt");
}

TEST(testInfoLevelWithWarningType)
{
    Logger* logger = new Logger(INFO);
    logger->log(WARNING, "%s", "Loading list");
    delete logger;
    char logType[20];
    getLoggerType(logType);
    CHECK_EQUAL("WARN:", logType);
    unlink("Logs.txt");
}

TEST(testWarningLevelWithDebugType)
{
    Logger* logger = new Logger(WARNING);
    logger->log(DEBUG, "%s", "Loading list");
    delete logger;
    char logType[20];
    getLoggerType(logType);
    CHECK(logType);
    unlink("Logs.txt");
}

TEST(testWarningLevelWithInfoType)
{
    Logger* logger = new Logger(WARNING);
    logger->log(INFO, "%s", "Loading list");
    delete logger;
    char logType[20];
    getLoggerType(logType);
    CHECK(logType);
    unlink("Logs.txt");
}

TEST(testWarningLevelWithWarningType)
{
    Logger* logger = new Logger(WARNING);
    logger->log(WARNING, "%s", "Loading list");
    delete logger;
    char logType[20];
    getLoggerType(logType);
    CHECK_EQUAL("WARN:", logType);
    unlink("Logs.txt");
}